package com.sdu.cloudservice.action;

import com.auth0.jwt.JWT;
import com.sdu.cloudservice.dto.Envelope;
import com.sdu.cloudservice.service.PredictPriceService;
import com.sdu.cloudservice.token.UserToken;
import com.sdu.cloudservice.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Create by zekdot on 19-7-4.
 */
@Controller
@CrossOrigin
@RequestMapping("/predict")
public class PredictAction {
    @Autowired
    private PredictPriceService predictPriceService;
    /**
     * 返回可用预测算法列表
     * @return
     */
    @RequestMapping("/algs")
    @ResponseBody
    @UserToken
    public String getAlgorithm(){
        try{
            return predictPriceService.getAlgorithm().toString();
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.systemError.toString();
        }
//        return new Envelope(3,"返回所有算法，未完成",null).toString();
    }

    /**
     * 返回未来两天的预测
     * @return
     */
    @RequestMapping("/future2Days")
    @ResponseBody
    @UserToken
    public String predictFuture(HttpServletRequest httpServletRequest, String zoneId, String instanceId, int algType){
        // 从 http 请求头中取出 token
        String token = httpServletRequest.getHeader("token");
        String username;
        // 如果是管理员
        if(PasswordUtil.getRole(token).equals("admin")){
            // 直接设置用户名为管理员
            username = "admin";
        }else{
            // 从token还原用户名
            username = JWT.decode(token).getAudience().get(0);
        }
        try{
            return predictPriceService.get2DayPredictPrice(zoneId,instanceId,algType,username).toString();
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.systemError.toString();
        }
//        return new Envelope(3,"返回未来两天",null).toString();
    }

    /**
     * 返回历史预测与实际价格
     * @return
     */
    @RequestMapping("/past3Days")
    @ResponseBody
    @UserToken
    public String predictPast(HttpServletRequest httpServletRequest,String zoneId,String instanceId,int algType){
        try{
            // 从 http 请求头中取出 token
            String token = httpServletRequest.getHeader("token");
            String username;
            // 如果是管理员
            if(PasswordUtil.getRole(token).equals("admin")){
                // 直接设置用户名为管理员
                username = "admin";
            }else{
                // 从token还原用户名
                username = JWT.decode(token).getAudience().get(0);
            }
            return predictPriceService.get3DayActPreDif(zoneId, instanceId, algType, username).toString();
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.systemError.toString();
        }
//        return new Envelope(3,"返回过去三天",null).toString();
    }
}
